<?php

namespace App\Repositories;



use App\Interfaces\ActivityService;
use App\Models\ActivityCat;
use App\Models\Activity;
use App\Models\ActivityGoods;
use App\Models\Goods;

class ActivityRepository implements ActivityService{

    protected  $activity;
    protected $error;


    public function __construct(Activity $activity){

        $this->activity = $activity;

    }

    public function getRecommendActivity(){


        return  $this->activity->where('is_recommend',1)->orderBy('sort_order','desc')->get()->take(4);

    }

    public function getActivity($activity_id){
        $activity = Activity::find($activity_id);
        return $activity;

    }

    public function getActivityByIdentification($identification){
        $activity = Activity::where('identification',$identification)->get();
        return $activity;

    }

    public function getActivityCat($activity_id){

         $activityCat = ActivityCat::where('activity_id',$activity_id)?ActivityCat::where('activity_id',$activity_id)->orderBy('sort_order','desc')->get():"";
    
         return $activityCat;

    }


    public function getActivityGoods($activity_cat_id){

        $activityGoods = ActivityGoods::select('activity_goods.*','goods.goods_thumb','goods.goods_name','goods.goods_pet_name','goods.market_price','goods.shop_price','goods.is_on_sale')
                                ->join('goods','activity_goods.goods_id','=','goods.goods_id')
                                ->where('activity_goods.activity_cat_id',$activity_cat_id)
                                ->where('goods.is_on_sale',1)
                                ->orderBy('activity_goods.sort_order','desc')->get();
        foreach ($activityGoods as $key=>$value) {
            $goods = Goods::find($value->goods_id);
            $activityGoods[$key]->mobile_goods_thumb = $goods->mobile_goods_thumb;
        }
        return $activityGoods;

       
    }

    public function getMobileActivityGoods($activity_cat_id,$page){

        $activityGoods = ActivityGoods::select('activity_goods.*','goods.goods_thumb','goods.goods_name','goods.goods_pet_name','goods.market_price','goods.shop_price','goods.is_on_sale')
            ->join('goods','activity_goods.goods_id','=','goods.goods_id')
            ->where('activity_goods.activity_cat_id',$activity_cat_id)
            ->where('goods.is_on_sale',1)
            ->orderBy('activity_goods.sort_order','desc')->paginate(8,['*'],'page',$page);
        foreach ($activityGoods as $key=>$value) {
            $goods = Goods::find($value->goods_id);
            $activityGoods[$key]->mobile_goods_thumb = $goods->mobile_goods_thumb;
        }
        return $activityGoods;


    }


}